<?php
namespace wap\controllers;

use Yii;
use common\helps\api;

/**
 * Site controller
 */

class InventoryController extends CommonController
{
    //选择模版
    public function actionIndex()
    {
        $data = Yii::$app->request->get();
        $pageFlg = isset($data['pageFlg']) ? $data['pageFlg'] : 0;
        $user = $this->getCache('USER');
        $result = api::post('inventory/temp-list', array('shopId' => $user['shopId']));

        if ($result['code'] == 200) {
            $result = $result['data'];
        } else {
            $result = array();
        }
        return $this->render('index', ['list' => $result, 'pageFlg' => $pageFlg]);
    }

    //盘点录入
    public function actionCountData()
    {
        $countId = null;
        $data = Yii::$app->request->get();
        $result = api::post('inventory/count-dtl', ['templateId' => $data['tempId']]);
        if ($result['code'] == 200) {
            $result = $result['data'];
            $countId = isset($result['countId']) ? $result['countId'] : null;
        } else {
            $result = array();
        }
        return $this->render('count-data', ['data' => $result, 'templateId' => $data['tempId'], 'countId' => $countId]);
    }

    //保存录入数据
    public function actionSaveCount()
    {
        $data = Yii::$app->request->post();
        $user = $this->getCache('USER');

        $param = array(
            'userId' => $user['userName'],
            'countId' => $data['countId'],
            'templateId' => $data['templateId'],
            'data' => isset($data['selectData']) ? $data['selectData'] : null
        );
        $result = api::post('inventory/add-count', $param);
        if ($result['code'] == 200) {
            $this->retJson('', 200);
        }
        $this->retJson('', 400);
    }

    //损耗录入
    public function actionLossData()
    {
        $lossId = null;
        $data = Yii::$app->request->get();
        $result = api::post('inventory/loss-dtl', ['templateId' => $data['tempId']]);
        if ($result['code'] == 200) {
            $result = $result['data'];
            $lossId = isset($result['lossId']) ? $result['lossId'] : null;
        } else {
            $result = array();
        }
        return $this->render('loss-data', ['data' => $result, 'templateId' => $data['tempId'], 'lossId' => $lossId]);
    }

    //保存录入数据
    public function actionSaveLoss()
    {
        $data = Yii::$app->request->post();
        $user = $this->getCache('USER');

        $param = array(
            'userId' => $user['userName'],
            'lossId' => $data['lossId'],
            'templateId' => $data['templateId'],
            'data' => isset($data['selectData']) ? $data['selectData'] : null
        );
        $result = api::post('inventory/add-loss', $param);
        if ($result['code'] == 200) {
            $this->retJson('', 200);
        }
        $this->retJson('', 400);
    }

    //模版
    public function actionTemplate()
    {
        $data = Yii::$app->request->get();
        if (isset($data['tempId']) && !empty($data['tempId'])) {
            $tempData = array(
                'tempId' => $data['tempId'],
                'tempName' => $data['tempName'],
                'shopId' => $data['shopId'],
                'shopName' => $data['shopName'],
                'remark' => $data['remark'],
            );
        } elseif (isset($data['pageFlg'])) {
            $tempData = $this->getCache('tempData');
        } else {
            $tempData = null;
        }
        $user = $this->getCache('USER');
        $shopArr = array();
        foreach ($user['shop'] as $shop) {
            $shopArr[] = array(
                'title' => $shop['shopName'],
                'value' => $shop['shopId'],
            );
        }
        return $this->render('template', ['shopArr' => $shopArr, 'tempData' => $tempData]);
    }

    //模版数据
    public function actionTemplateData()
    {
        $temp = null;
        $tempId = null;
        $editFlg = true;
        $tempDtl = array('mat' => null, 'pro' => null);

        $data = Yii::$app->request->get();
        if (isset($data['tempId']) && !empty($data['tempId'])) {

            $tempId = $data['tempId'];
            $result = api::post('inventory/temp-dtl', array('tempId' => $tempId));
            foreach ($result['data'] as $dtl) {
                $type = $dtl['type'] == 1 ? 'mat' : 'pro';
                $tempDtl[$type][$dtl['resId']] = 1;
            }
            if (!isset($data['editFlg'])) $editFlg = false;
        }
        if (isset($data['tempName'])) {
            $temp = array(
                'tempId' => $tempId,
                'tempName' => $data['tempName'],
                'shopId' => $data['shopId'],
                'shopName' => $data['shopName'],
                'remark' => $data['remark'],
            );
        }
        $this->setCache('tempData', $temp, 0);

        $result = api::post('inventory/data-list', array('shopId' => $data['shopId']));
        return $this->render('template-data', ['list' => $result['data'], 'tempDtl' => $tempDtl, 'editFlg' => $editFlg]);
    }

    public function actionSaveTemplate()
    {
        $data = Yii::$app->request->post();
        $user = $this->getCache('USER');
        $tempData = $this->getCache('tempData');

        $param = array(
            'userId' => $user['userName'],
            'tempId' => $tempData['tempId'],
            'shopId' => $tempData['shopId'],
            'tempName' => $tempData['tempName'],
            'remark' => $tempData['remark'],
            'data' => $data['selectData']
        );
        $result = api::post('inventory/add-template', $param);
        if ($result['code'] == 200) {
            $this->setCache('tempData', null, 0);
            $this->retJson('', 200);
        }
        $this->retJson('', 400);
    }

    //模版列表
    public function actionTemplateList()
    {
        $user = $this->getCache('USER');
        $result = api::post('inventory/temp-list', array('shopId' => $user['shopId']));

        if ($result['code'] == 200) {
            $result = $result['data'];
        } else {
            $result = array();
        }
        return $this->render('template-list', ['list' => $result]);
    }

    //删除模版
    public function actionDelTemp()
    {
        $data = Yii::$app->request->post();
        $param = array('tempId' => $data['tempId']);
        $result = api::post('inventory/del-temp', $param);
        if ($result['code'] == 200) {
            $this->retJson('', 200);
        }
        $this->retJson('', 400);
    }

}
